package com.example.listener;

import com.example.enums.BizCodeEnum;
import com.example.exception.BizException;
import com.example.model.EventMessage;
import com.example.service.TrafficService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/2214:26
 */
@Component
@RabbitListener(queuesToDeclare = {
        @Queue("order.traffic.queue"),
        @Queue("traffic.free_init.queue"),
        @Queue("traffic.release.queue")
})
@Slf4j
public class TrafficMQListener {

    @Autowired
    private TrafficService trafficService;

    @RabbitHandler
    public void trafficHandler(EventMessage eventMessage, Message message, Channel channel){
        log.info("监听到消息trafficHandler:{}",message);

        try {
            trafficService.handleTrafficMessage(eventMessage);
        }catch (Exception e){
            log.info("消费失败:{}",eventMessage);
            throw new BizException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
        }
        log.info("消费成功:{}",eventMessage);
    }
}
